/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MmsProductBaseInfoService
* 产品基础信息表新
*
* 修改纪录
* 2021-06-16 版本：1.0 hudeyong 创建。
* @version 版本：1.0
* @author 作者：hudeyong
* 创建日期：2021-06-16
*/
package com.topscomm.mms.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.mms.pojo.MmsDeptExtraInfoEntity;
import com.topscomm.mms.pojo.MmsProductBaseInfoEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.tap.common.RedisMmsStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mms.service.IMmsProductBaseInfoService;
import com.topscomm.mms.service.impl.auto.MmsProductBaseInfoServiceAuto;

import java.util.Map;

@Service(value = "mmsProductBaseInfoService")
@Scope(value = "singleton")
public class MmsProductBaseInfoService extends MmsProductBaseInfoServiceAuto implements IMmsProductBaseInfoService {
    @Autowired
    private RedisMmsStore redisMmscodeStore;
    @Autowired
    private MmsProductFloorPriceInfoService mmsProductFloorPriceInfoService;

    @Override
    protected void beforeInsert(BasicEntity entity) throws ServiceException {
        super.beforeInsert(entity);
    }

    @Override
    protected void updateBefore(Map<String, Object> map) throws ServiceException {
        MmsProductBaseInfoEntity oldInfo = this.queryById(ConvertUtil.convertToString(map.get("id")));
        if (oldInfo.getEnabled() == 1 && ConvertUtil.convertToInt(map.get("enabled")) == 0) {
            int enabledCount = mmsProductFloorPriceInfoService.getCount("  productid=" + oldInfo.getId() + " and enabled=1 ");
            if(enabledCount > 0) {
                throw new ServiceException("当前产品下存在未失效的小类，请先失效所有有效小类");
            }
            redisMmscodeStore.removeKey("productBaseInfo");
        }
    }

    @Override
    protected void updateAfter(Map<String, Object> map) throws ServiceException {
        super.updateAfter(map);
        this.addValueToRedis(MmsProductBaseInfoEntity.tableName);
    }

    @Override
    protected void afterInsert(BasicEntity entity) throws ServiceException {
        super.afterInsert(entity);
        //新增默认未启用，未发布
        entity.setValue(MmsProductBaseInfoEntity.FieldEnabled, 0);
        entity.setValue(MmsProductBaseInfoEntity.FieldPublishstate, "0");
        this.updateEntity(entity);
        this.addValueToRedis(MmsProductBaseInfoEntity.tableName);
    }

    @Override
    protected void deleteAfter(String whereSql) {
        super.deleteAfter(whereSql);
        this.addValueToRedis(MmsProductBaseInfoEntity.tableName);
    }

    /**
     * @description: 发布产品
     * @param id
     * @return void
     * @author: wangxinhua
     * @date: 2022/6/20 10:41
     * @modify:
     */
    @Override
    public void publishProduct(String id) {
        MmsProductBaseInfoEntity productBaseInfoEntity = this.queryById(id);
        if(productBaseInfoEntity == null) {
            throw new ServiceException("产品信息不存在，无法发布！");
        }
        productBaseInfoEntity.setPublishstate("1"); //发布
        productBaseInfoEntity.setEnabled(1); //启用
        this.updateEntity(productBaseInfoEntity);
        //清缓存
        redisMmscodeStore.removeKey("productBaseInfo");
    }
}
